//
//  MallOrderList.swift
//  LeGou
//
//  Created by lxm on 2022/8/5.
//

import Foundation
import HandyJSON
import UIKit


class RedBagProfitList: ZMViewController, UITableViewDelegate, UITableViewDataSource, ListRequestObjct {
    //MARK: --------------------属性--------------------
    var listData = [MineModels.ProfitList]()
    var page = 1
    var pageSize = 10
    var refreshScrollView : UIScrollView {
        tableView
    }
    var status : ProfitState = .all
    
    var type : ProfitIndexType = .redBag
    //MARK: --------------------UI控件--------------------
    let tableView = UITableView().then { t in
        t.separatorStyle = .none
        t.tableFooterView = UIView()
    }
    //MARK: --------------------生命周期--------------------
    override func viewDidLoad() {
        super.viewDidLoad()
        setUI()
        addConstraints()
        bindAction()
        tableView.mj_header?.beginRefreshing()
    }
    
    //MARK: --------------------UI--------------------
    func setUI() {
        title = "红包收益"
        setUpTableView()
        view.backgroundColor = "#F5F5F5".zm.hex()

    }
    
    func setUpTableView() {
        tableView.delegate = self
        tableView.dataSource = self
        tableView.backgroundColor = "#F5F5F5".zm.hex()
        tableView.zm.register(nibName: "PorfitListCell", reuserId: "PorfitListCell")
        
        view.addSubview(tableView)
        addRefresh(scrollView: tableView) { [weak self] refresh in
            guard let self = self else {return}
            if refresh {
                self.page = 1
            }
            self.loadList()
        }
        addNodataScrollView(scrollView: tableView)
        emptyTitle = "暂无数据"
        emptyImg = "nodata"
    }
    
    func addConstraints() {
        tableView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
    }
    
    //MARK: --------------------methods--------------------
    func bindAction() {
        ZMPayManager.shard.payCallBack.subscribe(onNext:{ [unowned self] resp in
            switch resp {
            case .success:
                self.tableView.mj_header?.beginRefreshing()
            default:
                ZMToast.showToast(title: resp.rawValue)
            }
        }).disposed(by: disposeBag)
        
        ZMNotification.shard.subject.subscribe(onNext:{ [unowned self] resp in
            switch resp {
            case .refreshOrderList:
                self.tableView.mj_header?.beginRefreshing()
            default:break
            }
        }).disposed(by: disposeBag)
    }
    
    func loadList() {
        var pa = ["limit" : pageSize, "page" : page, "type" : type.rawValue]
        if status != .all {
            pa["status"] = status.rawValue
        }
        ZMUtil.listRequest(type: MineModels.ProfitList.self, api: MineAPI.profitList(pa)) { resp in
            self.listRequest(resp)
        }
    }
    
    func cancelOrder(id : Int) {
        ZMAlertView.show(title: "提示", content: "是否要取消当前订单", left: "否", right: "是") {
            
        } onRight: {
            ZMToast.showActivity()
            VIPMallAPI.cancelOrder(["id" : id]).rxRequestNullAble(type: [String : Any].self).subscribe(onNext:{ [unowned self] resp in
                ZMToast.hiddenActivity()
                self.tableView.mj_header?.beginRefreshing()
            }).disposed(by: self.disposeBag)
        }
    }
    
    //  确认收货
    func ensureReceive(id : Int) {
        ZMAlertView.show(title: "提示", content: "是否确认收货", left: "否", right: "是") {
            
        } onRight: {
            ZMToast.showActivity()
            VIPMallAPI.suerReceive(id).rxRequestNullAble(type: [String : Any].self).subscribe(onNext:{ [unowned self] resp in
                ZMToast.hiddenActivity()
                self.tableView.mj_header?.beginRefreshing()
            }).disposed(by: self.disposeBag)
        }
    }
        
    // 跳转查看物流
    func goExpress(_ orderNo : String) {
        let vc = ZMLogisticsVC()
        vc.orderNo = orderNo
        navigationController?.pushViewController(vc, animated: true)
    }
    
    //  删除订单
    func ensureDelete(id : Int) {
        ZMAlertView.show(title: "提示", content: "是否要删除当前订单", left: "否", right: "是") {
            
        } onRight: {
            ZMToast.showActivity()
            VIPMallAPI.deleteOrder(id).rxRequestNullAble(type: [String : Any].self).subscribe(onNext:{ [unowned self] resp in
                ZMToast.hiddenActivity()
                self.tableView.mj_header?.beginRefreshing()
            }).disposed(by: self.disposeBag)
        }
    }
    
    //MARK: --------------------delegate--------------------
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 148
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        tableView.mj_footer?.isHidden = listData.count == 0
        return listData.count
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
 
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "PorfitListCell") as! PorfitListCell
        let data = listData[indexPath.row]
        cell.tbIcon.image = UIImage.init(named: "red_icon")
        cell.img.image = UIImage.init(named: "red_bag_icon")
        if data.redType == 3 {
            // 共享
            cell.name.text = "获得\(data.phone.zm.hidPhone())的共享红包"
        } else {
            cell.name.text = "抢到红包"
        }
        cell.orderNo.text = data.orderCreateTime
        cell.state.textColor = Style.mainColor
        cell.state.text = "¥\(data.income.zm.toString(keep: 4))"
        cell.tips.text = data.timeStr
        cell.partnerIcon.isHidden = true
        cell.tbIcon.image = UIImage.init(named: "red_icon")
        cell.order_new_icon.isHidden = true
        cell.refundBack.isHidden = true
        return cell
    }
    
}

